<?php

/**
 * user actions.
 *
 * @package    vmdai
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
  
  public function executeFind_user_info(sfWebRequest $request){
    if($request->isXmlHttpRequest() && $request->isMethod('POST')){
      
      $user_name = trim($request->getParameter('user_name',''));
      
      $user = Doctrine::getTable('User')->findOneByUserName($user_name);
      $result = array();
      
      if($user){
        
        $user_account = Doctrine::getTable('UserAccount')->findOneByUserId($user->getId());
        
        if(!$user_account){
           $result = array('status'=>2,'info'=>0);
           return $this->renderText(json_encode($result));
        }
        
        $result['status'] = 1;
        $result['info']['user_id'] = $user->getId();
        $result['info']['user_limit_money'] = $user_account->getSurplusQuota();
        
      }else{
        $result = array('status'=>0,'info'=>0);
      }
      
      return $this->renderText(json_encode($result));
    }
  }
  
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $dql = Doctrine::getTable('User')->createQuery()->orderBy('created_at desc');
      
      if($request->isMethod('POST')){
	  $user_name = $request->getParameter('user_name','');
	  $dql->where('user_name = ?',$user_name);
      }
        
	$page = $request->getParameter('page',1);
	$this->pager = new sfDoctrinePager('User',11);
	$this->pager->setQuery($dql);
	$this->pager->setPage($page);
	$this->pager->init();
  }
  
  //查看用户所有资料
  public function executeShow_user_info(sfWebRequest $request){
      $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById((int)$request->getParameter('id',0)));
      $this->loan_record = Doctrine::getTable('LoanRecord')->findByUserId($this->user->getId());
  }
  
  //增加限额
  public function executeAdd_quota(sfWebRequest $request){
    $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById((int)$request->getParameter('id',0)));
    $this->user_account = Doctrine::getTable('UserAccount')->findOneByUserId((int)$request->getParameter('id',0));
    $this->user_quotas = Doctrine::getTable('UserQuota')->findByUserId($this->user->getId());
  }
  
  //保存限额
  public function executeSave_quota(sfWebRequest $request){
    if($request->isMethod('POST')){
        $user_id = (int)$request->getParameter('user_id',0);
        $limit_money = (float)$request->getParameter('limit_money');
        $action_user_id = 0;
        
        if($limit_money < 1000){
            $this->getUser()->setFlash('error', '限额不能小于1000元！');
            $this->redirect('user/add_quota?id='.$user_id);
        }
        
        $user_quota = new UserQuota();
        $user_quota->setUserId($user_id)->setLimitMoney($limit_money)->setActionUserId($action_user_id)->save();
        $this->getUser()->setFlash('success', '限额添加成功！');
        $this->redirect('user/add_quota?id='.$user_id);
    }
  }
  
  
  public function executeEdit(sfWebRequest $request) {
    $this->user_id=(int)$request->getParameter('id',0);
    $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($this->user_id));
    $this->user_account=Doctrine::getTable('UserAccount')->findOneByUserId($this->user_id);
    $user_attr = Doctrine::getTable('UserAttr')->findOneByUserId($this->user_id);
    $this->userattrs=$user_attr->getUserAttrs();
    $this->edu_status=$user_attr::$edu_status;
    $this->has_cart_label=$user_attr::$has_cart_label;
    $this->marray_status=$user_attr::$marray_status;
    $this->residence_status=$user_attr::$residence_status;
    $this->relations_status=$user_attr::$relations_status;
  }
  
       public function executeSaveuserinfo(sfWebRequest $request){
     
       if( $request->isMethod("POST") ) {
         $user_id=(int)$request->getParameter('id',0);
        
         $arr=$request->getPostParameters();
         
         //更新用户信息
         $sex=$arr["sex"];
         $user = Doctrine::getTable('User')->findOneById($user_id);
         $user->setSex($sex)->setAge($arr["age"])->setRole($arr["role"])->setRegFrom($arr["regfrom"])->save();
         
         unset($arr["sex"]);
         unset($arr["age"]);
         unset($arr["role"]);
         unset($arr["regfrom"]);
         $user_attr=  Doctrine::getTable('UserAttr')->findOneByUserId($user_id);
        
         $user_attr->setUserAttrs($arr);
         
         
        $this->redirect('user/edit?id='.$user_id);
         
       }
    }
}
